package com.guojizhuang.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.guojizhuang.writable.MerchantInfoWritable;
import com.guojizhuang.writable.PredictionResultWritable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class testJson {
    public static void readFromHdfs(String hdfsUri) throws IOException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfsUri), conf);
        FSDataInputStream in = null;
        BufferedReader reader = null;
        List<PredictionResultWritable> predictionResultWritables=new ArrayList<>();
        try {
            in = fs.open(new Path(hdfsUri));
            reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); // 使用UTF-8编码

            String line;
            while ((line = reader.readLine()) != null) {
                String[] wordlist = line.toString().split("\t");
                if (wordlist.length==2)
                {
                    predictionResultWritables.add(new PredictionResultWritable(wordlist[0],wordlist[1].split(",")));
                }
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (in != null) {
                in.close();
            }
        }
        String jsonString = JSON.toJSONString(predictionResultWritables, SerializerFeature.PrettyFormat);
        try (FileWriter fileWriter = new FileWriter("/home/niit/json/guo_locations.json")) {
            // 将JSON字符串写入文件
            fileWriter.write(jsonString);
            fileWriter.flush();
            System.out.println("Person object has been written to 'person.json'");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
